/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.client.communication;

import core.messages.Msg;
import core.messages.server.creation.Msg_Role_Select_Require;
import core.messages.server.inGame.Msg_Card_Cast_Continue;
import core.messages.server.inGame.Msg_Duel_Continue;
import core.messages.server.inGame.Msg_Judge_Result;
import core.messages.server.inGame.Msg_Response_Accept;
import core.messages.server.inGame.Msg_Turn_Begin;
import core.threads.DataBase;
import core.threads.client.Client_Public_Data;
import core.threads.client.Client_Public_MsgBox;
import core.threads.client.Client_Public_Thread;
import core.threads.client.inGame.T_Client_Cast_Phase;
import core.threads.client.inGame.T_Client_Draw_Phase;
//import core.threads.client.inGame.T_Client_Duel;
import core.threads.client.inGame.T_Client_Judge_Phase;
import core.threads.client.inGame.T_Client_Responser;
import core.threads.client.inGame.T_Client_Role_Selector;
import cstAndEnum.JUDGE_TYPE;
import cstAndEnum.SKILL_TYPE;

/**
 *
 * @author Invince
 */
public class T_Client_Msg_Handler  extends Thread{
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;
/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        /**-----Loop----**/
        while(!this.stop){
            if(!this.suspend){
                if(!Client_Public_MsgBox.msgBox_Client_Rcv_Msg.isEmpty()){
                    Msg msg=Client_Public_MsgBox.msgBox_Client_Rcv_Msg.poll();
                    //Creation 部分
                    if(msg instanceof Msg_Role_Select_Require){
                        Client_Public_Thread.tClientRoleSelect=new T_Client_Role_Selector();
                        Client_Public_Thread.tClientRoleSelect.start();
                    }
                    
                    //inGame部分
                    else if(msg instanceof Msg_Turn_Begin){
                        Client_Public_Thread.tClientJudgePhase=new T_Client_Judge_Phase();
                        Client_Public_Thread.tClientJudgePhase.start();
                    }else if(msg instanceof Msg_Card_Cast_Continue){
                        Client_Public_Thread.tClientCastPhase=new T_Client_Cast_Phase();
                        Client_Public_Thread.tClientCastPhase.start();
                    }else if(msg instanceof Msg_Response_Accept){//包含被出决斗
                        Msg_Response_Accept msgResponseAccept=(Msg_Response_Accept) msg;
                        int idCard=msgResponseAccept.getIdCard();
                        int idSkill=msgResponseAccept.getIdSkill();
                        int idSource=msgResponseAccept.getIdSource();
                        int[] idTargets=msgResponseAccept.getIdTargetPlayers();
                        int[] idTargetPlayers=msgResponseAccept.getIdTargetCards();
                        SKILL_TYPE skillType=msgResponseAccept.getSkillType();
                        Client_Public_Thread.tClientResponse=new T_Client_Responser(idSkill, idSource, idTargets, idCard,idTargetPlayers, skillType);
                        Client_Public_Thread.tClientResponse.start();
                    }else if(msg instanceof Msg_Duel_Continue){//注意这里是继续决斗，决斗在上面
                        Msg_Duel_Continue msgDuelContinue=(Msg_Duel_Continue) msg;
                        int idCard=-1;//-1都是没用的东西
                        int idSkill=DataBase.findSkillByName("继续决斗").getId();
                        int idSource=msgDuelContinue.getIdSource();
                        int[] idTargets={msgDuelContinue.getIdTarget()};
                        SKILL_TYPE skillType=SKILL_TYPE.NORMAL_SKILL;
                        Client_Public_Thread.tClientResponse=new T_Client_Responser(idSkill, idSource, idTargets, idCard,null ,skillType);
                        Client_Public_Thread.tClientResponse.start();
                    }else if(msg instanceof Msg_Judge_Result){
                        Msg_Judge_Result msgJudgeResult=(Msg_Judge_Result) msg;
                        JUDGE_TYPE judgeType=msgJudgeResult.getJudgeType();
                        synchronized(Client_Public_Data.semaJudgeResult){
                            Client_Public_Data.judgeResult.set(judgeType, msgJudgeResult.isPassed());
                        }
                        int nbRestJudge;
                        synchronized(Client_Public_Data.semaNbRestJudge){
                            nbRestJudge=--Client_Public_Data.nbRestJudge;
                        }
                        if(nbRestJudge==0){
                            Client_Public_Thread.tClientDrawPhase=new T_Client_Draw_Phase();
                            Client_Public_Thread.tClientDrawPhase.start();
                        }
                    }
                    

                }
            }
        }
        /**----End-----**/
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Client_Msg_Handler() {
        synchronized(this){
            this.stop=false;
            this.suspend=false;
        }
    }
/*----------Gets and sets---------------*/

}
